package com.smileus.service;

/**
 * @Title: BaseService.java
 * @author dongheng
 * @date May 18, 2009 10:37:08 AM
 * @version V1.0
 * @hestory
 * 
 */

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;

import com.ibatis.common.util.Page;
import com.smileus.common.Env;
import com.smileus.dal.BaseHibernateDAO;

public class BaseService {
	protected static final Log logger = LogFactory.getLog(BaseService.class);
	protected static final BaseHibernateDAO defaultHibernateDAO = new BaseHibernateDAO(Env.getBean("sessionFactory", SessionFactory.class));
	//
	protected static final Map<String, BaseHibernateDAO> hibernateDAOMap = new HashMap<String, BaseHibernateDAO>();

	/**
	 * 获取默认的HibernateDAO
	 * 
	 * @return
	 */
	public BaseHibernateDAO getHibernateDAO() {
		return defaultHibernateDAO;
	}

	public BaseHibernateDAO getHibernateDAO(String sessionFactoryBeanId) {
		if (hibernateDAOMap.containsKey(sessionFactoryBeanId)) {
			return hibernateDAOMap.get(sessionFactoryBeanId);
		} else {
			hibernateDAOMap.put(sessionFactoryBeanId, new BaseHibernateDAO(Env.getBean(sessionFactoryBeanId, SessionFactory.class)));
		}
		return hibernateDAOMap.get(sessionFactoryBeanId);
	}

	/**
	 * save entity
	 * 
	 * @param <E>
	 *            // * @param entity
	 */
	public <E extends Serializable> void save(E entity) {
		getHibernateDAO().save(entity);
	}

	public <E extends Serializable> void saveOrUpdate(E entity) {
		getHibernateDAO().saveOrUpdate(entity);
	}

	/**
	 * remove entity
	 * 
	 * @param <E>
	 * @param entity
	 */
	public <E extends Serializable> void remove(E entity) {
		getHibernateDAO().remove(entity);
	}

	/**
	 * upate entity
	 * 
	 * @param <T>
	 * @param entity
	 */
	public <T> void update(T entity) {
		getHibernateDAO().update(entity);
	}

	/**
	 * 分页查找 ,使用方式：格式：from Person p where p.name =?�? 参数为变参，顺序与hql中的?顺序�?�?
	 * 
	 * @param <T>
	 * @param page
	 * @param hql
	 * @param param
	 * @return
	 */
	public <T> Page<T> findPageByArray(final Page<T> page, final String hql, final Object... param) {
		return getHibernateDAO().findPageByArray(page, hql, param);
	}

	/**
	 * 分页查找，使用方式：格式:from Person p where p.name
	 * 
	 * @param <T>
	 * @param page
	 * @param hql
	 * @param valueObject
	 * @return
	 */
	public <T> Page<T> findPageByValueBean(final Page<T> page, final String hql, final Object valueObject) {
		return getHibernateDAO().findPageByValueBean(page, hql, valueObject);
	}

	/**
	 * 注意：当记录不存在时候，get方法返回null
	 * 
	 * @param <T>
	 * @param entityClass
	 * @param id
	 * @return
	 */
	public <T> T get(Class<T> entityClass, Serializable id) {
		return getHibernateDAO().get(entityClass, id);
	}

	/**
	 * 注意：当记录不存在时候，产生异常
	 * 
	 * @param <T>
	 * @param entityClass
	 * @param id
	 * @return
	 */
	public <T> T load(Class<T> entityClass, Serializable id) {
		return getHibernateDAO().load(entityClass, id);
	}

	/**
	 * 查找所有实体
	 * 
	 * @param <E>
	 * @param entityClass
	 * @return
	 */
	public <E extends Serializable> List<E> queryAll(Class<E> entityClass) {
		return getHibernateDAO().queryAll(entityClass);
	}
}
